package com.bocai.yuweiwangluo.activity;


import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.RoundImageView;
import com.bocai.yuweiwangluo.View.XListView;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.presenter.TalkDetailPresenter;
import com.bocai.yuweiwangluo.viewModel.ITalkDetailView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 关于我们界面
 */
public class TalkDetailActivity extends BaseActivity implements ITalkDetailView,XListView.IXListViewListener,View.OnClickListener
{

    @ViewInject(R.id.lv_talk_detail)
    XListView lvTalkDetail;
    @ViewInject(R.id.et_comment)
    EditText etConmment;
    @ViewInject(R.id.tv_comment)
    TextView  tv_comment;
    View headerView;
    private TextView tvTitle,tvTime,tvName,tvCount,tvContent;
    private RoundImageView rivPhoto;
    String bbsId;
    private int page=1;
    private TalkDetailPresenter talkDetailPresenter;
    private boolean isRefresh=false;
    public static final int REQUST_OK=-1;
    @Override
    protected int contentView() {
        return R.layout.activity_talkdetail;
    }

    @Override
    protected void setData() {
        ViewUtils.inject(this);
        initToolbar("论坛");
        bbsId=getIntent().getStringExtra("id");
        initView();
        initEvent();
        getTalkDetailPresenter();
        getData();
    }

    private void initView() {
        headerView= LayoutInflater.from(this).inflate(R.layout.talk_detail_header,null);
        tvTitle=(TextView) headerView.findViewById(R.id.tv_talk_title);
        tvTime=(TextView) headerView.findViewById(R.id.tv_talk_time);
        rivPhoto=(RoundImageView)headerView.findViewById(R.id.riv_talk_photo);
        tvName=(TextView) headerView.findViewById(R.id.tv_talk_name);
        tvCount=(TextView) headerView.findViewById(R.id.tv_talk_count);
        tvContent=(TextView) headerView.findViewById(R.id.tv_content);
        lvTalkDetail.addHeaderView(headerView);
    }

    private void getData() {
        talkDetailPresenter.getBBSDetail();
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        tv_comment.setOnClickListener(this);
        lvTalkDetail.setPullLoadEnable(true);
        lvTalkDetail.setPullRefreshEnable(true);
        lvTalkDetail.setXListViewListener(this);
    }

    @Override
    public RoundImageView getRivPhoto() {
        return rivPhoto;
    }

    @Override
    public TextView getTvTitle() {
        return tvTitle;
    }

    @Override
    public TextView getTvTime() {
        return tvTime;
    }

    @Override
    public TextView getTvName() {
        return tvName;
    }

    @Override
    public TextView getTvNum() {
        return tvCount;
    }

    @Override
    public TextView getTvContent() {
        return tvContent;
    }

    @Override
    public String getbbsId() {
        return bbsId;
    }

    @Override
    public EditText getetComment() {
        return etConmment;
    }

    @Override
    public XListView getListView() {
        return lvTalkDetail;
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public void load() {
        lvTalkDetail.stopRefresh();
        lvTalkDetail.stopLoadMore();
        lvTalkDetail.setRefreshTime("");
    }

    @Override
    public void finishActivity() {
        if(isRefresh){
            Intent intent=new Intent();
            setResult(REQUST_OK,intent);
        }
        this.finish();
    }

    @Override
    public void callback() {
        isRefresh=true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            finishActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public TalkDetailPresenter getTalkDetailPresenter() {
        talkDetailPresenter=new TalkDetailPresenter(this,this,this);
        return talkDetailPresenter;
    }

    @Override
    public void onRefresh() {
        page=1;
        lvTalkDetail.setPullLoadEnable(true);
        talkDetailPresenter.getBBSDetail();

    }

    @Override
    public void onLoadMore() {
        page++;
        talkDetailPresenter.getBBSDetail();
    }

    @Override
    public void onClick(View v) {
        talkDetailPresenter.replyComment();
    }
}
